define(["angularAMD"], function (angularAMD) {
    angularAMD.service('CollectionService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {
            this.init = function(options){
                openDialog(options);
            };

            function openDialog(options){
                $mdDialog.dialog(
                    'module/modulesSPC/program/B0503/collection.tmp.html',
                    function(dialog){
                        return {
                            css_url:'module/modulesSPC/program/B0503/collection.css',
                            qc_form_no: options.qc_form_no || '',
                            plot_no: options.plot_no || '',
                            qty: options.qty || '',
                            inspect_type:  options.inspect_type || '',
                            inspect_no: options.inspect_no || '',
                            op_no: options.op_no || '',
                            factor_detail: options.factor_detail || [],
                            beforeShown: function(){
                                if(!dialog.factor_detail || dialog.factor_detail.length == 0){
                                    var content = {
                                        inspect_no: options.inspect_no || '',
                                        inspect_type: options.inspect_type || '',
                                        op_no: options.op_no || '',
                                        qc_form_no: options.qc_form_no || ''
                                    };
                                    factorInfoGet(content, function(data){
                                        if(data.factor_info_get.length > 0){
                                            dialog.factor_detail = data.factor_info_get;
                                        }
                                    });
                                }
                            },
                            loadFactorValue: function(item){
                                $rootScope.showQueryList({
                                    key:'parameter_no',
                                    header : [
                                        {label: $filter('translate')('collection.queryList.column1'), code:'parameter_no'},
                                        // {label: $filter('translate')('collection.queryList.column2'), code:'parameter_name'}
                                        // {label: $filter('translate')('collection.queryList.column3'), code:'memo'}
                                    ],
                                    query: function(conditions, feedback){
                                        var content = {
                                            factor_no: item.factor_no
                                        }
                                        factorOpenwinInfoGet(content, function(data){
                                            if(data.factor_openwin_info_get && data.factor_openwin_info_get.length > 0){
                                                feedback(data.factor_openwin_info_get);
                                            }else{
                                                feedback();
                                            }
                                        });
                                    },
                                    selectCode: '',
                                    confirm: function(selectItem, dialog){
                                        item.factor_value = selectItem.parameter_no;
                                        // item.factor_display = selectItem.parameter_name;
                                        dialog.hide();
                                    }
                                });
                            },
                            confirm : function(){
                                if(options.confirm){
                                    options.confirm(dialog);
                                } else {
                                    dialog.hide();
                                }
                            },
                            back : function(){
                                if(options.close){
                                    options.close(dialog);
                                } else {
                                    dialog.hide();
                                }
                            }
                        }
                    }
                )
            };
           
             //要因資訊取得
             function factorInfoGet(content, callback){
                // var data = {};
                // data.factor_info_get = [{
                //     factor_no: 'MTLVendor',
                //     factor_name: '供應商',
                //     factor_value: '',
                //     input_type: 1 
                // },{
                //     factor_no: 'Directions',
                //     factor_name: '方位',
                //     factor_value: '', 
                //     input_type: 0
                // }];

                console.log("要因資訊取得(factor_info_get) send:", content);
                // if(callback)
                //     callback(data);
                $MMWService.sendToSMESServer({
                	uri: 'tcSPC.clsSpc.factor_info_get',
                	content : content,
                	success : function(data){
                        console.log("要因資訊取得(factor_info_get) return:", data);
                		if(callback)
                			callback(data);
                	}
                });
            }

            //要因開窗資訊取得
            function factorOpenwinInfoGet(content, callback){
                // var data = [{
                //     parameter_no: 'reason01',
                //     parameter_name: 'Digiwin',
                //     memo: '鼎捷'
                // },{
                //     parameter_no: 'reason02',
                //     parameter_name: 'north',
                //     memo: '北方'
                // }];

                console.log("要因開窗資訊取得(factor_openwin_info_get) send:", content);
                // if(callback)
                //     callback(data);
                $MMWService.sendToSMESServer({
                	uri: 'tcSPC.clsSpc.factor_openwin_info_get',
                	content : content,
                	success : function(data){
                        console.log("要因開窗資訊取得(factor_openwin_info_get) return:", data);
                		if(callback)
                			callback(data);
                	}
                });
            }
        }
    ])
});
